﻿using System.Collections.Generic;
using AutoMapper;
using BestTrader.Cms.IServices;
using BestTrader.Cms.ViewModels;
using BestTrader.Cms.ViewModels.WebApi;
using BestTrader.Cms.WebApi.Validation;
using FluentValidation.Results;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace BestTrader.Cms.WebApi.Controllers.v1
{
    /// <summary>
    /// 首页管理
    /// </summary>
    [ApiVersion("1.0")]
    [Route("api/v{version:apiVersion}/[controller]")]
    [ApiController]
    public class HomeController : BaseController
    {
        #region BASE
        private readonly IHistory_DealedOrderReportService _historyOrderService;
        private readonly IRebateRecordService _rebateRecordService;
        private readonly IManagerService _managerService;
        private readonly IMapper _mapper;
        /// <summary>
        /// init
        /// </summary>
        /// <param name="historyOrderService"></param>
        /// <param name="rebateRecordService"></param>
        /// <param name="managerService"></param>
        /// <param name="mapper"></param>
        public HomeController(IHistory_DealedOrderReportService historyOrderService
            , IRebateRecordService rebateRecordService
            , IManagerService managerService
            , IMapper mapper)
        {
            _historyOrderService = historyOrderService;
            _rebateRecordService = rebateRecordService;
            _managerService = managerService;
            _mapper = mapper;
        }

        #endregion
        /// <summary>
        /// [10001] 首页数据
        /// </summary>
        /// 新增 costCoin  营业部成本金 
        /// <returns></returns>
        [HttpPost("GetHomeDataSet")]
#if !DEBUG
        [Authorize]
#endif
        public JsonResult GetHomeDataSet(HomeDataSetInput input)
        {

            var result = new TableDataResult<HomeDataSetOutput>();
          
            CurrentIdAndRoleIdValidation validationRules = new CurrentIdAndRoleIdValidation();
            ValidationResult results = validationRules.Validate(input);
            if (results.IsValid)
            {
                var _managerId = input.ManagerId;
                var _start = input.StartDate;
                var _end = input.EndDate.Date.AddDays(1).AddSeconds(-1);
                ///1 是结算日
                if (input.SearchDateType == "1")
                {
                    _start = input.StartDate.AddHours(6);
                    _end = input.EndDate.AddDays(1).AddHours(6).AddSeconds(-1);
                }
                var historyOrderData = _historyOrderService.GetDealedOrderReportByAgentId(_managerId, _start, _end);
                var coveOrderData = new HomeDealedOrderModel()
                {
                    TotalTrade = historyOrderData.Total,
                    ChartTitle = historyOrderData.ChartTitle,
                    ChartData = historyOrderData.ChartData
                };
                result.Data = new HomeDataSetOutput
                {
                    DealedOrder = coveOrderData,
                    Rebate = _rebateRecordService.GetAppRebateRecordByAgentId(_managerId, _start, _end,input.CurrentId.ToString(),input.RoleId.ToString()),
                };

                result.ResultCode = ResultCodeAddMsgKeys.CommonObjectSuccessCode;
                result.ResultMsg = ResultCodeAddMsgKeys.CommonObjectSuccessMsg;
            }
            else
            {
                result.ResultCode = ResultCodeAddMsgKeys.CommonModelStateInvalidCode;
                result.ResultMsg = results.ToString("||");
            }
            return Json<HomeDataSetOutput>(result.Data,result.Success,result.ResultCode,result.ResultMsg);

        }
        /// <summary>
        /// [10002] 代理商数据
        /// </summary>
        /// <returns></returns>
        [HttpPost("GetAgentName")]
        [Authorize]
        public JsonResult GetAgentName(AgentNameInput input)
        {
            var result = new BaseResult();
            var UserId = input.Uid;
            var RoleId = input.RoleId;

           var data =  _managerService.GetSelfAndChildManagerByManagerIdAndRoleId(UserId, RoleId, true);

            var newData = _mapper.Map<IEnumerable<AgentNameOutput>>(data);

            return Json<IEnumerable<AgentNameOutput>>(newData, result.Success,result.ResultCode,result.ResultMsg);

        }



    }
}
